const mongoose = require('mongoose')

const Image = mongoose.model('Image')

const Comment = mongoose.model('Comment')

const stats = () =>
  Promise.all([

    Image.aggregate([
        {$group: {
            _id: 'total',
            images: {$sum: 1},
            views: {$sum: '$views'},
            likes: {$sum: '$likes'}
            }
        }
    ]),
    Comment.count()
  ])

    .then(res => ({
      images: res[0][0] ? res[0][0].images: 0,
      views: res[0][0]  ? res[0][0].views: 0,
      likes: res[0][0]  ? res[0][0].likes: 0,
      comments:  res[1] ? res[1] :0
    }))

module.exports = stats
